package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    @GuiGuLogin
    @Operation(summary = "生成订单")
    @PostMapping("trade")
    public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo) {
        OrderInfoVo orderInfoVo =  orderInfoService.trade(tradeVo);
        return Result.ok(orderInfoVo);
    }

    @GuiGuLogin
    @Operation(summary = "提交订单")
    @PostMapping("submitOrder")
    public Result<Map<String, Object>> submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {
        String orderNo = orderInfoService.submitOrder(orderInfoVo);
        Map<String, Object> map = new HashMap<String , Object>();
        map.put("orderNo", orderNo);
        return Result.ok(map);
    }

    @Operation(summary = "根据订单号查询订单信息")
    @GetMapping("getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable("orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
        return Result.ok(orderInfo);
    }

    @Operation(summary = "根据用户id查询订单信息")
    @GuiGuLogin
    @GetMapping("findUserPage/{page}/{limit}")
    public Result<IPage<OrderInfo>> findUserPage(@PathVariable("page") Long pageNum,
                                                 @PathVariable("limit") Long PageSize) {
        IPage<OrderInfo> pageModel = orderInfoService.findUserPage(pageNum, PageSize);
        return Result.ok(pageModel);
    }

}

